package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.dto.OmsOrderDto;
import cn.zcscloud.mall.mbg.model.*;
import cn.zcscloud.mall.service.OmsOrderReturnApplyService;
import cn.zcscloud.mall.service.OmsOrderReturnReasonService;
import cn.zcscloud.mall.vo.OrderReturnApplyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/returnReason")
public class OmsOrderReturnReasonController {
    @Autowired
    private OmsOrderReturnReasonService returnReasonService;

    @Autowired
    private OmsOrderReturnApplyService orderReturnApplyService;

    //分页查询
    @GetMapping("/list")
    public R list(Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnReason> page = new Page<>(pageNum, pageSize);
        Page<OmsOrderReturnReason> reasonPage = returnReasonService.page(page);
        PageRequest pageRequest = PageRequest.init(reasonPage);
        return R.success(pageRequest);
    }

    //添加退货原因
    @PostMapping("/create")
    public R create(@RequestBody OmsOrderReturnReason returnReason) {
        String name = returnReason.getName();
        LambdaQueryWrapper<OmsOrderReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(returnReason.getName() != null, OmsOrderReturnReason::getName, returnReason.getName());
        OmsOrderReturnReason one = returnReasonService.getOne(queryWrapper);
        if (one != null) {
            return R.error("原因已经存在!", null);
        }
        returnReason.setCreateTime(new Date());
        returnReasonService.save(returnReason);
        return R.success("添加成功", null);
    }


    //批量删除退货原因
    @PostMapping("/delete")
    public R delete(@RequestParam List<Long> ids) {
        returnReasonService.removeByIds(ids);
        return R.success("删除成功!");
    }

    //获取单个退货原因详情信息
    @GetMapping("/{id}")
    public R getReturnReason(@PathVariable("id") Long id) {
        OmsOrderReturnReason returnReason = returnReasonService.getById(id);
        return R.success(returnReason);
    }

    //修改退货申请状态
    @PostMapping("/update/{id}")
    public R updateStatus(@RequestBody OmsOrderReturnReason returnReason) {

        returnReasonService.updateById(returnReason);
        return R.success("修改成功!");
    }

    //修改退货原因启用状态
    @PostMapping("/update/status")
    public R updateStatus(@RequestParam List<Long> ids, Integer status) {
        for (Long id : ids) {
            OmsOrderReturnReason reason = returnReasonService.getById(id);
            reason.setStatus(status);
            returnReasonService.updateById(reason);
        }
        return R.success("修改成功!");
    }

}
